package com.feihao.zhou.demoTest;


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;

/**
 * 1.	通过properties配置文件配置类的全限定名，配置方法名
 classname=xxxx
 methodname=eat
 */
public class Test1 {
    public static void main(String[] args) throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        //创建文件字节输入流读取配置文件:
        FileInputStream fis = new FileInputStream("D:\\IdeaProjects\\Job-code\\prop.properties");

        //创建Properties集合存储读取到的键值对:
        Properties pro = new Properties();
        pro.load(fis);

        //获取ClassName:
        String className = pro.getProperty("ClassName");
        //
        //获取class对象:
        Class aClass = Class.forName(className);
        //获取构造方法创建对象
//        TestStudent student = (TestStudent) aClass.newInstance();   //两种构造:
        Constructor constructor = aClass.getDeclaredConstructor(int.class, String.class, String.class);
        TestStudent student = (TestStudent) constructor.newInstance(18, "张三", "男");
        //设置学生姓名...年龄..
        student.setAge(18);
        student.setName("小三");
        student.setGender("男");
        //打印学生:
        System.out.println(student);




        //获取方法:
        String methodName = pro.getProperty("MethodName");
        //获取方法对象:
        Method method = aClass.getMethod(methodName);
        //创建学生对象:
        TestStudent student1 = (TestStudent) aClass.newInstance();
        student1.setName("小四");
        student1.setAge(19);
        student1.setGender("女");

        //调用方法:(无参无返回)
        method.invoke(student1);

    }
}
